package faf.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import faf.business.UserManager;
import faf.web.data.ResetForm;

public class ResetController extends SimpleFormController {
	UserManager userManager;

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ResetForm form = (ResetForm) command;
		
		if(userManager.resetPassword(form.getEmail())) {
			return new ModelAndView(new RedirectView(getSuccessView()));
		} else {
			errors.reject("email", "email-invalid");
			return showForm(request, response, errors);
		}
	}
}
